<!doctype html>
<html>
  <head>
    <title>Test Biquad Tail-Time</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit-util.js"></script>
    <script src="../resources/audit.js"></script>
    <script src="../resources/biquad-filters.js"></script>
    <script src="test-tail-time.js"></script>
  </head>

  <body>
    <script>
      let audit = Audit.createTaskRunner();

      let sampleRate = 16384;
      let renderSeconds = 1;

      // For a lowpass filter:
      //   b0 = (1-cos(w0))/2
      //   b1 = 1-cos(w0)
      //   b2 = (1-cos(w0))/2
      //   a0 = 1 + alpha
      //   a1 = -2*cos(w0)
      //   a2 = 1 - alpha
      //
      // where alpha = sin(w0)/(2*10^(Q/20)) and w0 = 2*%pi*f0/Fs.
      //
      // Equivalently a1 = -2*cos(w0)/(1+alpha), a2 = (1-alpha)/(1+alpha).  The
      // poles of this filter are at
      //
      //   cos(w0)/(1+alpha) +/- sqrt(alpha^2-sin(w0)^2)/(1+alpha)
      //
      // But alpha^2-sin(w0)^2 = sin(w0)^2*(1/4/10^(Q/10) - 1).  Thus the poles
      // are complex if 1/4/10^(Q/10) < 1; real distinct if 1/4/10^(Q/10) > 1;
      // and repeated if 1/4/10^(Q/10) = 1.

      // Array of tests to run.  |descripton| is the task description for
      // audit.define.  |parameters| is option for |testTailTime|.
      let tests = [
        {
          descripton:
              {label: 'lpf-complex-roots', description: 'complex roots'},
          sampleRate: sampleRate,
          renderDuration: renderSeconds,
          parameters: {
            prefix: 'LPF complex roots',
            filterOptions: {type: 'lowpass', Q: 40, frequency: sampleRate / 4}
          },
          // Node computed tail frame is 2079.4 which matches the real tail, so
          // tail output should be exactly 0.
          threshold: 0,
        },
        {
          descripton: {
            label: 'lpf-real-distinct-roots',
            description: 'real distinct roots'
          },
          sampleRate: sampleRate,
          renderDuration: renderSeconds,
          parameters: {
            prefix: 'LPF real distinct roots',
            filterOptions:
                {type: 'lowpass', Q: -50, frequency: sampleRate / 8}
          },
          // Node computed tail frame is 1699 which matches the real tail, so
          // tail output should be exactly 0.
          threshold: 0,
        },
        {
          descripton:
              {label: 'lpf-repeated-root', description: 'repeated real root'},
          sampleRate: sampleRate,
          renderDuration: renderSeconds,
          parameters: {
            prefix: 'LPF repeated roots (approximately)',
            // For a repeated root, we need 1/4/10^(Q/10) = 1, or Q =
            // -10*log(4)/log(10). This isn't exactly representable as a float,
            // we the roots might not actually be repeated.  In fact the roots
            // are actually complex at 6.402396e-5*exp(i*1.570796).
            filterOptions: {
              type: 'lowpass',
              Q: -10 * Math.log10(4),
              frequency: sampleRate / 4
            }
          },
          // Node computed tail frame is 2.9 which matches the real tail, so
          // tail output should be exactly 0.
          threshold: 0,
        },
        {
          descripton: {label: 'lpf-real-roots-2', description: 'complex roots'},
          sampleRate: sampleRate,
          renderDuration: renderSeconds,
          parameters: {
            prefix: 'LPF repeated roots 2',
            // This tests an extreme case where approximate impulse response is
            // h(n) = C*r^(n-1) and C < 1/32768.  Thus, the impulse response is
            // always less than the response threshold of 1/32768.
            filterOptions:
                {type: 'lowpass', Q: -100, frequency: sampleRate / 4}
          },
          // Node computed tail frame is 0 which matches the real tail, so
          // tail output should be exactly 0.
          threshold: 0,
        },
        {
          descripton: 'huge tail',
          // The BiquadFilter has an internal maximum tail of 30 sec so we want
          // to render for at least 30 sec to test this.  Use the smallest
          // sample rate we can to limit memory and CPU usage!
          sampleRate: 3000,
          renderDuration: 31,
          parameters: {
            prefix: 'LPF repeated roots (approximately)',
            hugeTaileTime: true,
            // For the record, for this lowpass filter, the computed tail time
            // is approximately 2830.23 sec, with poles at
            // 0.999998960442086*exp(i*0.209439510236777). This is very close to
            // being marginally stable.
            filterOptions: {
              type: 'lowpass',
              Q: 100,
              frequency: 100,
            },
            // Node computed tail frame is 8.49069e6 which is clamped to 30 sec
            // so tail output should be exactly 0 after 30 sec.
            threshold: 0,
          },
        },
        {
          descripton: 'ginormous tail',
          // Or this lowpass filter, the complex poles are actually computed to
          // be on the unit circle so the tail infinite.  This just tests that
          // nothing bad happens in computing the tail time. Thus, any small
          // sample rate and short duration for the test; the results aren't
          // really interesting. (But they must pass, of course!)
          sampleRate: 3000,
          renderDuration: 0.25,
          parameters: {
            prefix: 'LPF repeated roots (approximately)',
            filterOptions: {
              type: 'lowpass',
              Q: 500,
              frequency: 100,
            },
          },
          // Node computed tail frame is 90000 which matches the real tail, so
          // tail output should be exactly 0.
          threshold: 0,
        }
      ]

      // Define an appropriate task for each test.
      tests.forEach(entry => {
        audit.define(entry.descripton, (task, should) => {
          let context = new OfflineAudioContext(
              1, entry.renderDuration * entry.sampleRate, entry.sampleRate);
          testTailTime(should, context, entry.parameters)
              .then(() => task.done());
        });
      });

      audit.run();
    </script>
  </body>
</html>
